//index.js
//获取应用实例
const app = getApp()
var url = require('../../utils/url.js');
var page = 1;
var last_page = 0;
var type;
var user_id=0;
var loadMore = function (that) {
  wx.showLoading({
    title: '加载中',
  });
  if (page == 1) {
    page = 2
  }
  if (page < last_page || page == last_page) {
    console.log(page)
    wx.request({
      url: url.URl + 'index/goods',
      data: {page: page},
      method: 'get',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        that.setData({
          shops: that.data.shops.concat(res.data.data.data)
        });
        page++;
      }
    });
  } else {
    wx.hideLoading();
    that.setData({
      nodata: 1,
      skill: true
    })
  }
}
Page({
  data: {
    banner: [],
    nav:[],
    news:[],
    hot:[],
    Boutique:[],
    conscience:[],
    shops:[],
    meidenglu:true,
    indicatorDots: true,  //小点
    indicatorColor: "white",
    autoplay: true,  //是否自动轮播
    interval: 3000,  //间隔时间
    duration: 500,  //滑动时间
    indicatorActiveColor: "#DD2626",
  },
  onChange(e) {
    // console.log(e)
    this.setData({
      value: e.detail
    });
  },
  toSearch(event) {
    wx.navigateTo({
      url: '/pages/searchList/searchList?type=1',
    })
  },
  getGoods(){
    var that = this;
    if (page > 1) {
      page =1;
    }
    
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'index/goods',
      data: { page: page },
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var msg = res.data.msg;
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          that.setData({
            shops: res.data.data.data
          })
          wx.hideLoading()
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getAds(){
     var that=this;
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'index/ads',
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var msg = res.data.msg;
        console.log(res)
        if (res.data.code == 200) {
          console.log(res)
          that.setData({
            banner: res.data.data.ads1,
            hot: res.data.data.ads3,
            discounts: res.data.data.ads2,
            conscience: res.data.data.ads4,
            bigads: res.data.data.ads5,
            Boutique:res.data.data.ads6,
          })
          wx.hideLoading()
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getNews() {
    var that = this;
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'index/news',
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var msg = res.data.msg;
        if (res.data.code == 200) {
          console.log(res)
          that.setData({
             news:res.data.data
          })
          wx.hideLoading()
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getNav() {
    var that = this;
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'index/nav',
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var msg = res.data.msg;
        if (res.data.code == 200) {
          console.log(res)
          that.setData({
            nav:res.data.data
          })
          wx.hideLoading()
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // logCheck(token) {
  //   var that = this;
  //   wx.showLoading({
  //     title: '加载中..',
  //   })
  //   wx.request({
  //     url: url.URl + 'login/check',
  //     data: {},
  //     method: 'post',
  //     header: {
  //       'Authorization': token
  //     },
  //     success: function (res) {
  //       var msg = res.data.msg;
  //       wx.hideLoading()
  //       console.log(res)
  //       if (res.data.code == 200) {
         
          
          
  //       } else if(res.data==401){
  //         wx.showToast({
  //           title: msg,
  //           icon: 'none',
  //           duration: 2000
  //         })
  //       }
  //     }
  //   })
  // },
  onLoad: function (options){
    console.log(options)
    this.getGoods()
    this.getAds()
    this.getNews()
    this.getNav()
    if (options.userId) {
      wx.setStorage({
        key: 'rec_id',
        data: options.userId,
      })
    }
  },
  onUnload: function () {
    page = 1;
  },
  onReachBottom: function () {
    console.log(page)
    var that = this;
    if (that.data.index != 0) {
      loadMore(that)
    }
  },
  onPullDownRefresh() {
    var self = this;
    setTimeout(() => {
      // 模拟请求数据，并渲染
      self.onLoad()
      // 数据成功后，停止下拉刷新
      wx.stopPullDownRefresh();
    }, 1000);
  },
  onShareAppMessage: function (res) {
    console.log()
    if (res.from === 'button') {
    }
    return {
      title: '鱼跃龙门电商平台',
      path: '/pages/index/index?userId=' + user_id,
      success: function (res) {

      }
    }
  },
  scanCode: function (a) {
    wx.scanCode({
      //onlyFromCamera: true,
      success: (res) => {
        var path = res.path;
        wx.navigateTo({
          url: "/"+path,
        })
        console.log(res)
      }
    })
  },
  onShow(res){
    // console.log(res)
    var token;
    var that=this;
    wx.getStorage({
      key: 'token',
      success: function(res) {
        token=res.data;
        // that.logCheck(token) 
      },
    })
    wx.getStorage({
      key: 'userinfo',
      success: function (res) {
        user_id = res.data.id;
        // that.logCheck(token) 
      },
    })
  }
})
